﻿using HlElock.Tool.ViewModels;
using System;
using System.Windows;
using System.Windows.Interop;

namespace HlElock.Tool.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private HwndSource _hwndSource;
        private IntPtr? _winHandle;
        private readonly MainWindowViewModel _vm;
        public MainWindow()
        {
            InitializeComponent();
            SourceInitialized += MainWindow_SourceInitialized;
            _vm = new MainWindowViewModel();
            DataContext = _vm;
        }

        private void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            _hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            _hwndSource?.AddHook(WndProc);
            _winHandle = _hwndSource?.Handle;

        }
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            try
            {
                if (msg == 0x219)
                {
                    switch (wParam.ToInt32())
                    {
                        case 0x0007:
                            _vm.RefreshUseHid();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return hwnd;
        }
    }
}
